﻿using BrnMall.Manager.Domain.AddressBook;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BrnMall.Manager.Manager.AddressBook
{
    public class DepartmentManager
    {
        static string TableName = "crm_Department";

        public static List<Department> GetList(int storeId, int pageSize = 50)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<Department>(string.Format("select top {0} * from {1} where storeId={2} order by sort desc", pageSize, TableName, storeId));
            }
        }

        public static List<Department> GetListByAmount(int storeId)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<Department>(string.Format("select * from {0} where storeId={1}", TableName, storeId));
            }
        }

        public static Department GetInfo(int Id) {
            using (var db = new QDMallDatabase())
            {
                return db.SingleOrDefault<Department>(string.Format("select * from {0} where Id={1}", TableName, Id));
            }
        }

        public static List<Department> GetLists(int storeId) {
            using (var db = new QDMallDatabase())
            {
                return db.Fetch<Department>(string.Format("select * from {0} where storeId={1}", TableName, storeId));
            }
        }


        public static int Add(Department model)
        {
            using (var db = new QDMallDatabase())
            {
                return (int)db.Insert(model);
            }
        }


        public static int Update(int Id,string value) {
            using (var db = new QDMallDatabase()) {
                return db.Execute(string.Format("update {0} set Name='{1}' where Id={2}", TableName, value, Id));
            }
        }

        public static bool Update(Department model)
        {
            using (var db = new QDMallDatabase())
            {
                return db.Update(model) > 0;
            }
        }

        public static int Delete(int Id) {
            using (var db = new QDMallDatabase())
            {
                return db.Execute(string.Format("delete from {0} where Id={1}", TableName, Id));
            }
        }
    }
}
